package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.cache.CacheData;
import pers.cj.pdos.cache.CacheRemove;
import pers.cj.pdos.cache.CacheRemoveMore;
import pers.cj.pdos.entity.Custom;
import pers.cj.pdos.entity.SettAccount;
import pers.cj.pdos.mapper.SettAccountMapper;
import pers.cj.pdos.service.ISettAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.SettAccountVo;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class SettAccountServiceImpl extends ServiceImpl<SettAccountMapper, SettAccount> implements ISettAccountService {
    @Autowired
    SettAccountMapper settAccountMapper;

    @Override
    public List<SettAccountVo> listVoPage(Integer page, Integer limit, String companyId, String content, String storeId) {
        return settAccountMapper.listVo(Utils.genPage(page,limit),limit,companyId, Utils.genLikeData(content),storeId);
    }

    @Override
    public List<SettAccountVo> listVo(String companyId, String storeId) {
        return settAccountMapper.listVo(null,null,companyId, null,storeId);
    }

    @Override
    public long count(String companyId, String searchContent, String storeId) {
        return settAccountMapper.count(companyId,searchContent,storeId);
    }

    @Override
    public List<SettAccountVo> listVoForInit(Integer page, Integer limit, String companyId, String search) {
        return settAccountMapper.listVoForInit(Utils.genPage(page,limit), limit, companyId, search);
    }

    @Override
    public long countForInit(String companyId, String search) {
        return settAccountMapper.countForInit(companyId,search);
    }

    @Override
    public boolean checkName(String name, String companyId) {
        return settAccountMapper.selectCount(new QueryWrapper<SettAccount>().eq("name",name).eq("company_id",companyId)) > 0;
    }

    @Override
    public SettAccountVo getVoById(String id) {
        return settAccountMapper.getVoById(id);
    }

    @Override
    public void insert(SettAccount settAccount) {
        if (settAccount.getDef()) {
            SettAccount noDef = new SettAccount();
            noDef.setDef(false);
            settAccountMapper.update(noDef, new QueryWrapper<SettAccount>().eq("company_id", settAccount.getCompanyId()).eq("def",true));
        }
        settAccountMapper.insert(settAccount);
    }

    @Override
    public void remove(SettAccount settAccount) {
        settAccountMapper.deleteById(settAccount.getId());
    }

    @Override
    public void update(SettAccount settAccount) {
        if (settAccount.getDef()) {
            SettAccount noDef = new SettAccount();
            noDef.setDef(false);
            settAccountMapper.update(noDef, new QueryWrapper<SettAccount>().eq("company_id", settAccount.getCompanyId()).eq("def",true));
        }
        settAccountMapper.updateById(settAccount);
    }

    @Override
    public List<SettAccount> getByStore(String storeId, String companyId) {
        return settAccountMapper.selectList(new QueryWrapper<SettAccount>().eq("store_id",storeId).eq("company_id",companyId));
    }

    @Override
    public BigDecimal getTotalBalance(String companyId) {
        return settAccountMapper.getTotalBalance(companyId);
    }

}
